//
//  MyOrderViewController.m
//  DuBai
//
//  Created by darktm on 14-3-11.
//  Copyright (c) 2014年 darktm. All rights reserved.
//

#import "MyOrderViewController.h"

#import "MyOrderViewCell.h"


#import "TaskWithGetOrders.h"

#import "UIImageView+WebCache.h"

#import "MyGradeViewController.h"
#import "CommodityCommentsViewController.h"

@interface MyOrderViewController ()<WYHttpTaskDelegate>

@property(nonatomic,strong)NSMutableArray* ordersArray;

@end

@implementation MyOrderViewController

#define TAG_CELL_BTN 10000

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    [self.allBtn setTitleColor:[UIColor brownColor] forState:UIControlStateSelected];
    [self.allBtn setTitleColor:[UIColor brownColor] forState:UIControlStateHighlighted];
    [self.allBtn setSelected:YES];
    
    [self.unPaybtn setTitleColor:[UIColor brownColor] forState:UIControlStateSelected];
    [self.unPaybtn setTitleColor:[UIColor brownColor] forState:UIControlStateHighlighted];

    
    [self.finishBtn setTitleColor:[UIColor brownColor] forState:UIControlStateSelected];
    [self.finishBtn setTitleColor:[UIColor brownColor] forState:UIControlStateHighlighted];
    [self loadOrdersWithStatus:OrderStateAll];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view =[[UIView alloc]init];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    Orders *order = self.ordersArray[indexPath.row];
    int state = [order.status intValue];
    
    if(state == OrderStateWaitSend || state == OrderStateWaitPaid)
        return 140.0f;
    else
        return 111.0f;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.ordersArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyOrderViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyOrderViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    Orders *order = self.ordersArray[indexPath.row];
    [cell.image setImageWithURL:[NSURL URLWithString:order.icon]];
    cell.desc.text = order.desc;
    cell.sales.text = order.sales;
    cell.status.text = order.status;
    cell.price.text = order.price;

    int state = [order.status intValue];
    
    switch (state) {
        case OrderStateWaitPaid:
            cell.more_name.text = @"付款剩余时间";
            cell.more_value.text = @"付款剩余时间";
            [cell.btn setTitle:@"付款" forState:UIControlStateNormal];
            [cell.btn setBackgroundColor:[UIColor redColor]];
            break;
            
        case OrderStateWaitSend:
            cell.more_name.text = @"发货时间";
            cell.more_value.text = @"发货时间";
            [cell.btn setTitle:@"评价" forState:UIControlStateNormal];
            [cell.btn setBackgroundColor:[UIColor brownColor]];
            break;
        case OrderStateCancel:
            [cell.btn setTitle:@"已取消" forState:UIControlStateNormal];
            break;
    }
    
    cell.btn.tag = TAG_CELL_BTN + indexPath.row;
    [cell.btn addTarget:self action:@selector(on_btn_cell_btn:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - http delegate -

-(void)requestCompleteWithObj:(WYHttpTask *)obj{
    
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    
    id rsult = [obj parese];
    
    if([obj isKindOfClass:[TaskWithGetOrders class]]){
        self.ordersArray = rsult;
        [self.tableView reloadData];
    }}
-(void)requestFailWithMsg:(WYHttpTask *)obj{
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    DLog(@"%@",obj.networkOp.url);
}

#pragma mark - customer method -

-(void)loadOrdersWithStatus:(int) status{
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    if(self.ordersArray){
        [self.ordersArray removeAllObjects];
    }else{
        self.ordersArray = [NSMutableArray array];
    }
    [self.tableView reloadData];
    
    TaskWithGetOrders *order = [[TaskWithGetOrders alloc] init];
    order.state = [NSString stringWithFormat:@"%d",status];
    order.delegate = self;
    [order start];
}

#pragma mark - btn actions -

-(void)on_btn_cell_btn:(UIButton *)btn{
    
    int tag = btn.tag - TAG_CELL_BTN;
    
    UIStoryboard *s = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    CommodityCommentsViewController * vc = [s instantiateViewControllerWithIdentifier:@"CommodityCommentsViewController"];
    
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)on_btn_unPay:(UIButton *)sender {
    if(sender.selected) return;
    
    sender.selected = YES;
    self.allBtn.selected = NO;
    self.finishBtn.selected = NO;
    
    [self loadOrdersWithStatus:OrderStateWaitPaid];
    
    [self promptyAnimationIndex:1];
}

- (IBAction)on_btn_finish:(UIButton *)sender {
    if(sender.selected) return;
    
    sender.selected = YES;
    self.allBtn.selected = NO;
    self.unPaybtn.selected = NO;
    
    [self loadOrdersWithStatus:OrderStateReceived];
    
    [self promptyAnimationIndex:2];
}
- (IBAction)on_btn_all:(UIButton *)sender {
    if(sender.selected) return;
    
    sender.selected = YES;
    self.unPaybtn.selected = NO;
    self.finishBtn.selected = NO;
    
    [self loadOrdersWithStatus:OrderStateAll];
    
    [self promptyAnimationIndex:0];
}


-(void)promptyAnimationIndex:(int)index{
    CGRect frame = self.prompty.frame;
    frame.origin.x = 15 + index * 107;
    
    [UIView animateWithDuration:.2 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        self.prompty.frame = frame;
    } completion:NULL];
}
@end
